package com.wn.budget.controller;

import com.wn.budget.model.MaterialDepot;
import com.wn.budget.repository.MaterialDepotRepository;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("materialDepot")
public class MaterialDepotController {

    @Autowired
    MaterialDepotRepository materialDepotRepository;

    @PostMapping("insert")
    public boolean insert(MaterialDepot materialDepot) {
        materialDepot.setId(UUID.randomUUID().toString());
        materialDepot.setCreateDate(new Date());
        materialDepotRepository.saveAndFlush(materialDepot);
        return true;
    }

    @ApiOperation("更新材料库名称")
    @PostMapping("update")
    public boolean update(MaterialDepot materialDepot) {
        MaterialDepot _m = materialDepotRepository.getOne(materialDepot.getId());
        _m.setName(materialDepot.getName());
        materialDepotRepository.saveAndFlush(_m);
        return true;
    }

    /**
     * 材料库树结构数据
     *
     * @return List<MaterialDepot>
     */
    @GetMapping("findAll")
    public List<MaterialDepot> findAll() {
        return materialDepotRepository.findAll();
    }

    @ApiOperation(value = "获取材料库列表")
    @GetMapping("findById")
    public MaterialDepot findById(@RequestParam("id") String id) {
        return materialDepotRepository.findById(id).get();
    }

    @GetMapping("deleteById")
    public void deleteById(@RequestParam("id") String id) {
        materialDepotRepository.deleteById(id);
    }

}
